package com.atguigu.yygh.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.util.HttpUtil;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.service.IDepartmentService;
import com.atguigu.yygh.service.IHospitalService;
import com.atguigu.yygh.service.IHospitalSetService;
import com.atguigu.yygh.service.IScheduleService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @create 2023-02-25 20:03
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private IHospitalService hospitalService;
    @Resource
    private IHospitalSetService hospitalSetService;
    @Resource
    private IDepartmentService departmentService;
    @Resource
    private IScheduleService scheduleService;


    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        SignCheck(map);

        String hosScheduleId = (String) map.get("hosScheduleId");

        scheduleService.remove(hosScheduleId);

        return Result.ok();

    }

    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换一下类型
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        SignCheck(map);

        scheduleService.saveSchedule(map);

        return Result.ok();
    }


    @PostMapping("/schedule/list")
    public Result ScheduleList(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        SignCheck(map);
        String hoscode = (String) map.get("hoscode");
        Integer page =  (String) map.get("page")==null ? 1 :Integer.parseInt((String) map.get("page"));
        Integer limit =  (String) map.get("limit")==null ? 1 : Integer.parseInt((String) map.get("limit"));
        Page<Schedule> list=scheduleService.getSchedule(hoscode,page,limit);
        return Result.ok(list);
    }



    @PostMapping("/department/remove")
    public Result remove(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        SignCheck(map);
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");

        departmentService.remove(hoscode, depcode);

        return Result.ok();

    }

    @PostMapping("/department/list")
    public Result list(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        SignCheck(map);
        String hoscode = (String) map.get("hoscode");
        Integer page =  (String) map.get("page")==null ? 1 :Integer.parseInt((String) map.get("page"));
        Integer limit =  (String) map.get("limit")==null ? 1 : Integer.parseInt((String) map.get("limit"));


        Page<Department> list=departmentService.getDepartment(hoscode,page,limit);


        return Result.ok(list);
    }


    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        SignCheck(map);
        String hoscode = (String) map.get("hoscode");


        Hospital hospital=hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);


    }

    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换一下类型
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        SignCheck(map);

        hospitalService.saveHospital(map);

        return Result.ok();
    }

    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //转换一下类型
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        SignCheck(map);


        departmentService.saveDepartment(map);

        return Result.ok();
    }

    private void SignCheck(Map<String, Object> map){
        String hoscode = (String) map.get("hoscode");
        String sign = (String) map.get("sign");
        String SignKey=hospitalSetService.getSignKey(hoscode);

        String encrypt = MD5.encrypt(SignKey);
        if(!encrypt.equals(sign)){
            //不匹配
            throw new YyghException(ResultCodeEnum.SIGN_ERROR.getMessage(),ResultCodeEnum.SIGN_ERROR.getCode());
        }
    }
}
